package com.atguigu.crud.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.atguigu.crud.bean.Wxmenu;
import com.atguigu.crud.bean.WxmenuExample;
import com.atguigu.crud.bean.WxmenuExample.Criteria;
import com.atguigu.crud.dao.WxmenuMapper;
import com.atguigu.weixin.kit.WeixinKit;
import com.atguigu.weixin.model.WeixinFianlValue;
import com.atguigu.weixin.model.WeixinMenuDto;

@Service("wxmenuService")
public class WxmenuService {

	@Autowired
	private	WxmenuMapper wxmenuMapper;
	//查询对象条件
	public Wxmenu getwxmenu(String key){
		Wxmenu w=new Wxmenu();
		WxmenuExample we=new WxmenuExample();
		Criteria criteria = we.createCriteria();
		criteria.andMenukeyEqualTo(key);
		w=wxmenuMapper.selectByExample(we).get(0);
		 return w;
	}
	
	
	//发布生成微信菜单接口
	public void publishMenu(){
		String url=WeixinKit.replaceAccToken(WeixinFianlValue.MENU_ADD);
		System.out.println("替换后的"+url);
		List<WeixinMenuDto> wmds=new ArrayList<WeixinMenuDto>();
		wmds=this.generateWeixinMenuDto();//封装查询所有微信菜单
		Map<String,List<WeixinMenuDto>> maps=new HashMap<String,List<WeixinMenuDto>>();
		maps.put("button", wmds);
		String json=JSON.toJSONString(maps);
		System.out.println("输入的："+json);
		String rel=WeixinKit.sendJsonPost(url, JSON.toJSONString(maps));
		
		if(!WeixinKit.checkRequestSuccess(rel)){
			throw new RuntimeException("发布菜单失败："+WeixinKit.getRequestMsg(rel));
		}else{
		  System.out.println("布菜单："+WeixinKit.getRequestMsg(rel));	
		}
		
	};
	
	//查询微信菜单接口
	public String queryMenu(){
		String url=WeixinKit.replaceAccToken(WeixinFianlValue.MENU_QUERY);
		return WeixinKit.sendGet(url);
	}
	/**
	 * 按照员工id查询菜单
	 * @param id
	 * @return
	 */
	public Wxmenu getWxmenu(Integer id) {
		Wxmenu Wxmenu = wxmenuMapper.selectByPrimaryKey(id);
		return Wxmenu;
	}
	
	/**
	 * 查询所有菜单
	 * @return
	 */
	public List<Wxmenu> getAll() {
		return wxmenuMapper.selectByExample(null);
	}
	

  
	/**
	 * 保存
	 * @param Wxmenu
	 */
	public void saveWxmenu(Wxmenu wxmenu){
		if(wxmenu.getType().equals("click"))
			wxmenu.setMenukey("KEY_"+System.currentTimeMillis());
		wxmenuMapper.insertSelective(wxmenu);
	}
	
	
	/**
	 * 更新
	 * @param Wxmenu
	 */
	public void updateWxmenu(Wxmenu Wxmenu) {
		// TODO Auto-generated method stub
		wxmenuMapper.updateByPrimaryKeySelective(Wxmenu);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteWxmenu(Integer id) {
		// TODO Auto-generated method stub
		wxmenuMapper.deleteByPrimaryKey(id);
	}
	//批量删除
	public void deleteBatch(List<Integer> ids) {
		// TODO Auto-generated method stub
		WxmenuExample example = new WxmenuExample();
		Criteria criteria = example.createCriteria();
		//delete from xxx where emp_id in(1,2,3)
		criteria.andIdIn(ids);
		wxmenuMapper.deleteByExample(example);
	}
	
	//微信接口数据接口
	public List<WeixinMenuDto> generateWeixinMenuDto() {
		List<Wxmenu> menus = this.getAll();
		List<WeixinMenuDto> menuDtos = new ArrayList<WeixinMenuDto>();
		WeixinMenuDto wmd = null;
		for(Wxmenu wm:menus) {
			wmd = new WeixinMenuDto();
			wmd.setKey(wm.getMenukey());
			wmd.setName(wm.getName());
			wmd.setType(wm.getType());
			wmd.setId(wm.getId());
			wmd.setUrl(wm.getUrl());
			if(wm.getPid()==null||wm.getPid()==0) {
				if(wmd.getSub_button()==null) {
					wmd.setSub_button(new ArrayList<WeixinMenuDto>());
				}
				menuDtos.add(wmd);
			} else {
				WeixinMenuDto twmd = findById(wm.getPid(), menuDtos);
				if(twmd==null) throw new RuntimeException("菜单的父类对象有问题，请检查");
				twmd.getSub_button().add(wmd);
			}
		}
		return menuDtos;
	}
	
	private WeixinMenuDto findById(int id,List<WeixinMenuDto> wmds) {
		for(WeixinMenuDto wmd:wmds) {
			if(wmd.getId()==id) {
				return wmd;
			}
		}
		return null;
	}
	
}
